<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/5
+--------------------------------------------------------------
 * Time: 9:42
+--------------------------------------------------------------
 * StatusAction 状态列表
+--------------------------------------------------------------
 */
class StatusAction extends BaseAction{

    protected  $page_size = 20;

    public function _initialize(){
        self::$action = 3;
        parent::_initialize();
        $this->assign('tag_style',C('tag_style'));
    }

    public function index(){

        $problem_id = !empty($_GET['problem_id']) ? $this->filterId($_GET['problem_id']) : '';
        $user_id    = !empty($_GET['user_id']) ? $this->filterUserid($_GET['user_id']):'';
        $language   = (isset($_GET['language']) && (-1!==intval($_GET['language']))) ? intval($_GET['language']): -1;  //I('get.language',false,'htmlspecialchars');
        $result     = (isset($_GET['result']) && (-1!==intval($_GET['result'])))  ? intval($_GET['result']) : -1;        //I('get.result',false,'htmlspecialchars');
        $sub_order      = !empty($_GET['order']) ? intval($_GET['order']) : 1;//1倒序,2顺序
//        dump($_GET['start_date']);
//        dump($_GET['end_date']);
        $start_date = !empty($_GET['start_date']) ? date('Y-m-d H:i:s',$_GET['start_date']) : '';
        $end_date   = !empty($_GET['end_date']) ? date('Y-m-d H:i:s',$_GET['end_date']) : '';
        $table = ' solution as a';
        $fields= ' a.* ';
        $where = ' a.problem_id !=0 ';
        $group = ' group by a.solution_id ';
        $order = (1===$sub_order) ? ' order by a.in_date DESC ': ' order by a.in_date ASC ';

        if(!empty($user_id)){
            $where .= " and a.user_id='".$user_id."' ";
        }
        if((-1!==$language)){
            $where .= " and a.language=".$language." ";
        }
        if((-1 !==$result)){
            $where .= " and a.result=".$result." ";
        }
        if(!empty($start_date) && !empty($end_date)){
            $where .= " and unix_timestamp(a.in_date) between ".strtotime($start_date)." and ".strtotime($end_date)." ";
        }

        if(!empty($problem_id)){
            $where .= " and a.problem_id in(".$problem_id.") ";
        }
        $sql_total = "select count(*) as total from {$table} where {$where}";
        $res = D('Solution')->query($sql_total);
        $count = !empty($res) ? $res[0]['total']:0;
        vendor("Page");
//      import("ORG.Util.Page");
        $page = new Page($count,$this->page_size);

        $sql   = "select {$fields} from {$table} where {$where} {$group} {$order} limit ".$page->firstRow.",".$page->listRows;
        $list  = D('Solution')->query($sql);
//        echo D('Solution')->_sql();
//        dump($list);

        $this->assign('problem_id',$problem_id);
        $this->assign('user_id',$user_id);
        $this->assign('language',$language);
        $this->assign('result',$result);
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->assign('lang_list',C('language'));
        $this->assign('result_list',C('result'));
        $this->display('index');
    }
}